package com.api.customization.unionpay.market.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.MarketingProjectInfoService;
import com.customization.unionpay.market.service.impl.MarketingProjectInfoServiceImpl;
import com.customization.unionpay.market.util.FormDataUtils;
import com.engine.common.util.ServiceUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

/**
 * @author sjm
 * @description 营销项目信息台账相关
 */
@Path("/market/project")
public class MarketingProjectInfoController {

    private static final Logger log = LoggerFactory.getLogger(MarketingProjectInfoController.class);

    private MarketingProjectInfoService getService(User user) {
        return ServiceUtil.getService(MarketingProjectInfoServiceImpl.class, user);
    }

    // 提供接口接收营销项目管理平台推送的数据,存入营销项目信息台账
    @POST
    @Path("/add/info")
    @Produces(MediaType.APPLICATION_JSON)
    public String addProjectInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "添加成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);

            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);

                // 项目编号
                String projectCode = parseJson.getString("PROJECT_CODE");
                // 项目名称
                String projectName = parseJson.getString("PROJECT_NAME");
                // 主办单位
                String organizer = parseJson.getString("ORGANIZER");
                // 主办人
                String sponsor = parseJson.getString("SPONSOR");
                // 产品
                String project = parseJson.getString("PROJECT");
                // 场景
                String scene = parseJson.getString("SCENE");
                // 是否缴纳个税	 1-有发票 2-部分有发票3-无发票时
                String payPersonalTax = parseJson.getString("PAY_PERSONAL_TAX");
                // 	个税金额       没有金额时传：待缴个税金额
                String personalTaxAt = parseJson.getString("PERSONAL_TAX_AT");
                // 出资方式 下拉框
                String contributionType = parseJson.getString("CONTRIBUTION_TYPE");
                // 银联出资金额
                String unionpayCtbtAt = parseJson.getString("UNIONPAY_CTBT_AT");
                // 外部出资金额
                String extCtbtAt = parseJson.getString("EXT_CTBT_AT");
                // 合作方类型
                String partnerType = parseJson.getString("PARTNER_TYPE");
                // 合作方名称
                String partnerName = parseJson.getString("PARTNER_NAME");

                String source = parseJson.getString("SOURCE");
                String time = parseJson.getString("TIME");

                if (StrUtil.isBlank(projectCode)) {
                    result.put("msg", "项目编号为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(projectName)) {
                    result.put("msg", "项目名称为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(organizer)) {
                    result.put("msg", "主办单位为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(sponsor)) {
                    result.put("msg", "主办人为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(project)) {
                    result.put("msg", "产品为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(scene)) {
                    result.put("msg", "场景为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(personalTaxAt)) {
                    result.put("msg", "个税金额为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(contributionType)) {
                    result.put("msg", "出资方式为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(unionpayCtbtAt)) {
                    result.put("msg", "银联出资金额为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(extCtbtAt)) {
                    result.put("msg", "外部出资金额为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(partnerType)) {
                    result.put("msg", "合作方类型为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(partnerName)) {
                    result.put("msg", "合作方名称为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }

                if (StrUtil.isBlank(source)) {
                    result.put("msg", "来源系统为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(time)) {
                    result.put("msg", "请求时间为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }

                getService(user).addProjectInfo(projectCode, projectName, organizer, sponsor, project,scene,payPersonalTax,personalTaxAt,contributionType, unionpayCtbtAt, extCtbtAt, partnerType, partnerName);
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }

        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！=>"+e.getMessage());
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    // 提供实时查询财务系统项目结项的结项状态和项目维度信息的接口
    @POST
    @Path("/get/info")
    @Produces(MediaType.APPLICATION_JSON)
    public String getProjectInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "查询成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);

            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);

                // 项目编号
                String projectCode = parseJson.getString("PROJECT_CODE");
                String source = parseJson.getString("SOURCE");
                String time = parseJson.getString("TIME");

                if (StrUtil.isBlank(projectCode)) {
                    result.put("msg", "项目编号为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(source)) {
                    result.put("msg", "来源系统为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(time)) {
                    result.put("msg", "请求时间为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }

                JSONObject resultData = getService(user).getProjectInfo(projectCode);
                if(resultData == null){
                    result.put("msg", "获取项目信息数据为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                }else {
                    result.put("data", resultData);
                }
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！");
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }
}
